#include "fixedcombobox.h"

FixedComboBox::FixedComboBox(QWidget *parent)
    : QComboBox(parent)
{
    // 用 QListView 替换默认视图，启用滚动条
    QListView *lv = new QListView(this);
    lv->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setView(lv);

    // 指定最多显示 6 行
    setMaxVisibleItems(6);
}

void FixedComboBox::showPopup()
{
    int rowHeight = view()->sizeHintForRow(0);
    if (rowHeight <= 0)
        rowHeight = this->sizeHint().height();

    int visibleCount = qMin(count(), maxVisibleItems());
    int popupHeight = rowHeight * visibleCount;

    view()->setFixedHeight(popupHeight);  // 只限高，不动位置

    QComboBox::showPopup(); // 原始逻辑决定坐标与宽度
}
